<?php
/**
 * Blog post
 *
 * @copyright        2017 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           Sam Chen <sam.chen@opencart.cn>
 * @created          2017-05-22 14:10:24
 * @modified         2017-05-26 17:17:27
 */

class ControllerBlogPost extends Controller {
	public function index() {
		if (!$this->config->get('blog_status')) {
			return $this->notFound();
		}

		$this->load->language('blog/blog');

		if (isset($this->request->get['blog_post_id'])) {
		  $post_id = $this->request->get['blog_post_id'];
		} else {
			$post_id = 0;
		}

		if (isset($this->request->get['blog_category_id'])) {
		  $category_id = $this->request->get['blog_category_id'];
		} else {
			$category_id = 0;
		}

		if (isset($this->request->get['route'])) {
			$this->document->addLink(HTTP_SERVER, 'canonical');
		}

		$data['text_author'] = $this->language->get('text_author');
		$data['text_date_added'] = $this->language->get('text_date_added');
		$data['text_title'] = $this->language->get('text_title');
		$data['text_permalink'] = $this->language->get('text_permalink');
		$data['text_category'] = $this->language->get('text_category');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		);

		$menu_name = $this->config->get('blog_menu_name');
		if(isset($menu_name[$this->config->get('config_language_id')])) {
		  $text_blog = $menu_name[$this->config->get('config_language_id')];
		} else {
		  $text_blog = 'Blog';
		}
		$data['breadcrumbs'][] = array(
			'text' => $text_blog,
			'href' => $this->url->link('blog/home')
		);

		$this->load->model('blog/blog');
		$post_info = $this->model_blog_blog->getPost($post_id);

		if (!$post_info) {
			return $this->notFound();
		}

		if ($category_id) {
			$category_info = $this->model_blog_blog->getCategory($category_id);

			if ($category_info) {
				$data['breadcrumbs'][] = array(
					'text' => $category_info['name'],
					'href' => $this->url->link('blog/category', 'blog_category_id=' . $category_info['category_id'])
				);
			}
		} else {
			$category_info = array();
		}

		// Post category list
		$data['categories'] = array();
		$results = $this->model_blog_blog->getPostCategories($post_id);
		if ($results) {
			foreach ($results as $result) {
				$data['categories'][] = array(
					'name' => $result['name'],
					'href' => $this->url->link('blog/category',  'blog_category_id=' . $result['category_id'])
				);
			}
		}

		$authors = $this->config->get('blog_default_author');
		if (isset($authors[$this->config->get('config_language_id')])) {
			$default_author = $authors[$this->config->get('config_language_id')];
		} else {
			$default_author = $this->language->get('config_name');
		}

		if (!empty($post_info['meta_title'])) {
			$this->document->setTitle($post_info['meta_title']);
		} else {
			$this->document->setTitle($post_info['name']);
		}

		$data['heading_title'] = $post_info['name'];

		$data['breadcrumbs'][] = array(
			'text' => $post_info['name'],
			'href' => $this->url->link('blog/post', ($category_id ? "blog_category_id={$category_id}&" : '') . 'blog_post_id=' . $post_id),
		);

		if (!empty($post_info['meta_description'])) {
			$this->document->setDescription($post_info['meta_description']);
		}

		if (!empty($post_info['meta_keyword'])) {
			$this->document->setKeywords($post_info['meta_keyword']);
		}

		if (!empty($post_info['description'])) {
			$data['description'] = html_entity_decode($post_info['description'], ENT_QUOTES, 'UTF-8');
		} else {
			$data['description'] = '';
		}

		$data['author'] = $post_info['author'] ? $post_info['author'] : $default_author;
		$data['date_added'] = date($this->language->get('date_format_short'), strtotime($post_info['date_added']));
		$data['content'] = html_entity_decode($post_info['content'], ENT_QUOTES, 'UTF-8');

		if ($category_info) {
			$data['href'] = $this->url->link('blog/post', "blog_category_id={$category_id}&blog_post_id={$post_info['post_id']}");
		} else {
			$data['href'] = $this->url->link('blog/post', 'blog_post_id=' . $post_info['post_id']);
		}

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		$this->response->setOutput($this->load->view('/blog/post.tpl', $data));
	}

	private function notFound() {
		$this->load->language('blog/blog');

		$data['breadcrumbs'] = array();
		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		);

		$this->document->setTitle($this->language->get('text_error'));
		$data['heading_title'] = $this->language->get('text_error');
		$data['text_error'] = $this->language->get('text_error');
		$data['button_continue'] = $this->language->get('button_continue');
		$data['continue'] = $this->url->link('common/home');

		$this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		$this->response->setOutput($this->load->view('error/not_found', $data));
	}
}
